package com.vanguard.controller;

import com.vanguard.commons.base.BaseController;
import com.vanguard.commons.page.PageQuery;
import com.vanguard.commons.page.PageResult;
import com.vanguard.commons.response.CommonReturnCode;
import com.vanguard.commons.response.HttpResult;
import com.vanguard.domain.User;
import com.vanguard.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Title: 用户操作前端控制层
 * @Description:
 * @Author: vanguard
 * @Version: 1.0
 * @Date: 2019/03/19
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @PostMapping("/save")
    public HttpResult save(@RequestBody User user) {
        userService.save(user);
        return new HttpResult(CommonReturnCode.OK);
    }

    @GetMapping()
    public HttpResult getAll(PageQuery pageQuery) {
        PageResult result = userService.getAll(pageQuery);
        return new HttpResult(CommonReturnCode.SUCCESS, result);
    }

    @GetMapping("/view/{id}")
    public HttpResult view(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        return new HttpResult(CommonReturnCode.OK, user);
    }

    @DeleteMapping("/delete/{id}")
    public HttpResult delete(@PathVariable("id") Long id) {
        userService.deleteById(id);
        return new HttpResult(CommonReturnCode.OK);
    }

}
